
<%@ page language="java" contentType="text/html" 
    pageEncoding="UTF-8" %>
    

<%@ page import="javax.xml.transform.*" %>
<%@ page import="javax.xml.transform.stream.*" %>
<%@ page import="javax.xml.parsers.*" %>
<%@ page import="org.w3c.dom.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.net.URL" %>
<%@ page import="java.net.MalformedURLException" %>

<%@ page import="java.net.HttpURLConnection" %>

<% 

  String itemId = request.getParameter("item_id");
  URL url_item = new URL("http://api.powerhousemuseum.com/api/v1/item/" + itemId +"/xml/?api_key=90b0723f9926ce3");
  HttpURLConnection conn = (HttpURLConnection) url_item.openConnection();

  //InputStream file = conn.getInputStream();
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
  
  Document doc_item = db.parse(conn.getInputStream());
  
  /* Get multimedia url */
  NodeList multimediaList = doc_item.getElementsByTagName("multimedia_uri");
  Node multimedia = multimediaList.item(0).getFirstChild();
  String multimediaUri = multimedia.getNodeValue();
  URL url_multimedia = new URL("http://api.powerhousemuseum.com"+multimediaUri);
  
  HttpURLConnection conn_multimedia = (HttpURLConnection) url_multimedia.openConnection();
  Document doc_multimedia = db.parse(conn_multimedia.getInputStream());
  
  /*
  NodeList thumbnails = doc_item.getElementsByTagName("thumbnail");
  String thumbnail_url = thumbnails.item(0).getFirstChild().getFirstChild().getNodeValue();
	*/
  //Using xslt 
  Transformer transformer = TransformerFactory .newInstance().newTransformer(new StreamSource(application.getResourceAsStream("item.xsl")));	
  transformer.transform(new StreamSource(url_item.openStream()), new StreamResult(out));
  
  

  
  
  
  
  
  
  
  
  
  
  
  
  
  
  /* Get multimedia image url */
  String[] thumbnail_urls;
  int num_multimedia = Integer.parseInt(doc_item.getElementsByTagName("num_multimedia").item(0).getFirstChild().getNodeValue());
  //out.println(num_multimedia);
  NodeList multimedia_thumbnails = doc_multimedia.getElementsByTagName("thumbnail");
  
  //out.println(multimedia_thumbnails);
  //int totalThumbnail = multimedia_thumbnails.getLength();
  //out.print(totalThumbnail);
  
  for(int i=0; i<num_multimedia; i++)
  {  
	  thumbnail_urls = new String[num_multimedia];
	  
	  thumbnail_urls[i] =  multimedia_thumbnails.item(i).getFirstChild().getFirstChild().getNodeValue();
	  //out.println(thumbnail_urls[i]);  
	  
	  out.print("<img src="+thumbnail_urls[i]+" />");
  }
  
  
  
  
  
  
  
  
  /*
  NodeList subjects = doc.getElementsByTagName("subjects");
  NodeList subjectNodes = subjects.item(0).getChildNodes();
  int totalSubject = subjectNodes.getLength();
 
  String[] subjectList = new String[totalSubject];;
  for(int i=0; i<totalSubject; i++)
  {  
	  subjectList[i] =  subjectNodes.item(i).getFirstChild().getNodeValue();
	  out.println(subjectList[i]);  
  }
  
  URL url_subject = new URL("http://api.powerhousemuseum.com/api/v1/subject/xml/?api_key=90b0723f9926ce3&name_exact="+subjectList[0].replace(" ","%20"));
  HttpURLConnection conn_subject = (HttpURLConnection) url_subject.openConnection();
 
  Document doc_subject = db.parse(conn_subject.getInputStream());
  
  NodeList itemsApi = doc_subject.getElementsByTagName("items_uri");
  String itemsUrl= itemsApi.item(0).getFirstChild().getNodeValue();
  //int totalApi = itemsApiNodes.getLength();
 
  out.println(itemsUrl);
  //out.print(totalApi);
  //String itemsUrl = itemsApiNodes.item(0).getFirstChild().getNodeValue();
  //out.println(itemsUrl);
 
  
  */
  /*
  NodeList names = doc.getElementsByTagName("names");
  
  NodeList itemNodes = names.item(0).getChildNodes();
  
  int totalItem = itemNodes.getLength();
  String[] types = {"",""};
  for(int i=0; i<totalItem; i++)
  {
	  
	  types[i] =  itemNodes.item(i).getFirstChild().getNodeValue();
  }  
  
  for(int i=0; i<types.length; i++)
  {
	  
	  out.println(types[i]);
  }  
  
  out.println("Total no of item : " + totalItem);
  out.println("--------------------------->");
  
  */
  
  //URL url_similar_item = new URL("http://api.powerhousemuseum.com/api/v1/item-name/xml/?api_key=90b0723f9926ce3&name=" + types[0]);
  //HttpURLConnection conn_similar_item = (HttpURLConnection) url_similar_item.openConnection();
  //Document doc_similar_item = db.parse(conn_similar_item.getInputStream());
  //traverseTree(doc_similar_item, out);
%>

<%! 
  /** 
	Handles one node of the tree.  It accepts any type of node, and
	will check the node type before processing it.  This function 
	is recursive - if one node contains other "sub-nodes", this
	function will call itself again to process the sub-nodes.

	@param currnode the current node
	@param out      where to write the output
  */
  private void traverseTree(Node currnode, JspWriter out) throws Exception {

		
		 
		  
		  
    // If the current node is null, do nothing
    if(currnode == null) {
      return;
    }

    // Find the type of the current node
    int type = currnode.getNodeType();

    // Check the node type, and process it accordingly
    switch (type) {

    /*
	 * Handle the top-level document node.
	 * Just print out the word "DOCUMENT", and then get the
	 * root element of the document, and process it using
	 * the traverseTree method
     */
    case Node.DOCUMENT_NODE:
    {
      	
      out.println("<p>Categories:</p>");
      traverseTree (((Document)currnode).getDocumentElement(), out);
      break;
    }
    
    
    /*
	 * Handle an element node
	 * This is the most complex type of node dealt with.
	 * First, print out the name of the element, before
	 * processing any other sub-nodes (i.e. a preorder traversal).
	 * Secondly, check if this element has any attributes, and
	 * if it does, process those next, by calling the traverseTree()
	 * method.
	 * Finally, retrieve the children of this node (if any), and
	 * process them one by one using the traverseTree() method.
     */
    case Node.ELEMENT_NODE:
    {
      String elementName = currnode.getNodeName();
      //out.println("<p>ELEMENT: [" + elementName + "]</p>");
      
      if (currnode.hasAttributes()) {
        NamedNodeMap attributes = currnode.getAttributes();
        for (int i=0; i < attributes.getLength(); i++) {
          Node currattr = attributes.item(i);
          traverseTree(currattr, out);
        }
      }
    
      NodeList childNodes = currnode.getChildNodes();      
      if(childNodes != null) {
        for (int i=0; i < childNodes.getLength() ; i++) {
          traverseTree (childNodes.item(i), out);
        }
      }
      break;
    }
    
    
    /*
	 * Handle attribute nodes.
	 * Just print out the word "ATTRIBUTE", and then the name
	 * and value of the attribute itself.
     */
    case Node.ATTRIBUTE_NODE:
    {
      String attributeName = currnode.getNodeName();
      String attributeValue = currnode.getNodeValue();
      if(attributeName.equals("name"))
      //out.println("<p>ATTRIBUTE: name=[" + attributeName +
      //"], value=[" + attributeValue + "]</p>");
      break;
    }
    
    
    /*
	 * Handle text nodes.
	 * Trim whitespace off the beginning and end of the text.
	 * Then check whether there is any real text, and if so,
	 * print it out.  This avoids printing out text nodes that
	 * consist of only whitespace characters.
     */
    case Node.TEXT_NODE: {
      String text = currnode.getNodeValue().trim();
      if (text.length() > 0) {
        out.println("<p>TEXT: [" + text + "]</p>");
      }
      break;
    }
  }
}

%>

